package lazy2.tool.beangenerator;

import java.sql.*;
import lazy2.util.StringUtil;

public enum DBType {
	MSSQLServer, MySQL;
	private static void close(ResultSet resultSet, Statement statement) {
		if (resultSet!=null) {
			try {
				resultSet.close();
			} catch (SQLException e) {
				//e.printStackTrace();
			}
		}
		if (statement!=null) {
			try {
				statement.close();
			} catch (SQLException e) {
				//e.printStackTrace();
			}
		}
	}
	public static PrimaryKeyInfo isPrimaryKey(DBType dbType, Connection connection, String tableName, String columnName) throws SQLException {
		if(dbType==null
		|| connection==null
		|| connection.isClosed()
		|| StringUtil.isEmptyIgnoreWhitSpaces(tableName)
		|| StringUtil.isEmptyIgnoreWhitSpaces(columnName))
			return null;
				
		PreparedStatement stmt = null;
		ResultSet rs = null;
		String sql = null;
		String colAutoIncrement = null;
		String valAutoIncrement = null;
		switch(dbType) {
		case MSSQLServer:
			sql = "SELECT C.* FROM SYS.INDEXES I JOIN SYS.INDEX_COLUMNS IC ON I.OBJECT_ID=IC.OBJECT_ID AND I.INDEX_ID=IC.INDEX_ID JOIN SYS.COLUMNS C ON I.OBJECT_ID=C.OBJECT_ID AND IC.COLUMN_ID=C.COLUMN_ID WHERE IS_PRIMARY_KEY=1 AND I.OBJECT_ID=OBJECT_ID(?) AND COL_NAME(I.OBJECT_ID,C.COLUMN_ID)=?";
			colAutoIncrement = "is_identity";
			valAutoIncrement = "1";
			break;
		case MySQL:
			sql = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_KEY='PRI' AND TABLE_NAME=? AND COLUMN_NAME=?";
			colAutoIncrement = "EXTRA";
			valAutoIncrement = "auto_increment";
			break;
		}
		if(StringUtil.hasText(sql)) {
			try {
				stmt = connection.prepareStatement(sql);
				stmt.setString(1, tableName);
				stmt.setString(2, columnName);
				rs = stmt.executeQuery();
				if(rs.next()) {
					PrimaryKeyInfo pkInfo = new PrimaryKeyInfo();
					pkInfo.identity = valAutoIncrement.equalsIgnoreCase(rs.getString(colAutoIncrement));
					return pkInfo;
				}
			} finally {
				close(rs, stmt);
			}
		}
		
		return null;
	}
	public PrimaryKeyInfo isPrimaryKey(Connection connection, String tableName, String columnName) throws SQLException {
		return DBType.isPrimaryKey(this, connection, tableName, columnName);
	}
	public static ResultSetMetaData getTableMetaData(DBType dbType, Connection connection, String tableName) throws SQLException {
		if(dbType==null
		|| connection==null
		|| connection.isClosed()
		|| StringUtil.isEmptyIgnoreWhitSpaces(tableName))
			return null;
		
		String sql = null;
		
		switch (dbType) {
		case MSSQLServer:
			sql = "SELECT TOP 1 * FROM ["+tableName+"]";
			break;
		case MySQL:
			sql = "SELECT * FROM `"+tableName+"` LIMIT 1";
			break;
		}
		
		if(sql!=null) {
			PreparedStatement stmt = null;
			ResultSet         rs   = null;
			try {
				stmt = connection.prepareStatement(sql);
				rs = stmt.executeQuery();
				return rs.getMetaData();
			} finally {
				close(rs, stmt);
			}			
		}
		
		return null;

	}
	public ResultSetMetaData getTableMetaData(Connection connection, String tableName) throws SQLException {
		return getTableMetaData(this, connection, tableName);
	}
}
